package cn.lyh.edu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.lyh.exception.BadRequestException;
import cn.lyh.edu.domain.EduTeacher;
import cn.lyh.edu.repository.EduTeacherRepository;
import cn.lyh.edu.service.IEduTeacherService;
import cn.lyh.edu.service.dto.TeacherQueryCriteria;
import cn.lyh.utils.PageUtil;
import cn.lyh.utils.QueryHelp;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**功能描述：教师业务接口实现类

 */
@Service
@Transactional(readOnly = true)
public class EduTeacherServiceImpl implements IEduTeacherService {

    private final EduTeacherRepository eduTeacherRepository;

    public EduTeacherServiceImpl(EduTeacherRepository eduTeacherRepository) {
        this.eduTeacherRepository = eduTeacherRepository;
    }

    /**
     * 获取教师列表数据
     * @param queryCriteria
     * @param pageable
     * @return
     */
    @Override
    public Object getList(TeacherQueryCriteria queryCriteria, Pageable pageable) {
        Page<EduTeacher> page = eduTeacherRepository.findAll((root, query, criteriaBuilder)->
                QueryHelp.getPredicate(root,queryCriteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page);
    }

    /**
     * 新增教师信息
     * @param eduTeacher
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addTeacher(EduTeacher eduTeacher) {
        // 根据教师号查询记录
        EduTeacher dbEduTeacher = eduTeacherRepository.findByTeacherNo(eduTeacher.getTeacherNo());
        if(dbEduTeacher!=null){
            throw new BadRequestException("添加失败，教师号已经存在！");
        }else {
            eduTeacherRepository.save(eduTeacher);
        }

    }

    /**
     * 根据ID获取详情信息
     * @param id
     * @return
     */
    @Override
    public EduTeacher getById(Long id) {
        return eduTeacherRepository.findById(id).orElseGet(EduTeacher::new);
    }

    /**
     * 更新教师信息
     * @param eduTeacher
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editTeacher(EduTeacher eduTeacher) {

        // 根据教师ID获取教师信息
        EduTeacher dbEduTeacher = eduTeacherRepository.getReferenceById(eduTeacher.getId());

        // 根据教师号获取教师信息
        EduTeacher tempEduTeacher = eduTeacherRepository.findByTeacherNo(eduTeacher.getTeacherNo());
        if(tempEduTeacher!=null&&!tempEduTeacher.getId().equals(eduTeacher.getId())){
            throw new BadRequestException("更新失败，教师号已经存在！");
        }else {
            BeanUtil.copyProperties(eduTeacher,dbEduTeacher, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
            eduTeacherRepository.save(dbEduTeacher);
        }

    }

    /**
     * 根据ID删除信息
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(Long id) {
        eduTeacherRepository.deleteById(id);
    }

    /**
     * 获取所有教师信息
     * @return
     */
    @Override
    public List<EduTeacher> getAll() {
        return eduTeacherRepository.findAll();
    }
}
